<?php

namespace App\Http\Controllers;

use App\Models\Msgs;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use EasyWeChat\Kernel\Messages\Text;
use EasyWeChat\Factory;
use Symfony\Component\Cache\Simple\RedisCache;
/**
 * 消息推送
 * Class CateController
 * @package App\Http\Controllers
 */
class PushController extends Controller{


    public function index(){
//        array (
//            'ToUserName' => 'gh_c04bdfcb5dbf',
//            'FromUserName' => 'oOM3i5JP--2n6cszwPrjILJxSMkQ',
//            'CreateTime' => 1541129600,
//            'MsgType' => 'text',
//            'Content' => '浏览器',
//            'MsgId' => 6619101231350326229,
//        )
        $app = app('wechat.mini_program');
        $predis = app('redis')->connection()->client();
        $cache = new RedisCache($predis);
        $app->rebind('cache', $cache);
        $app->server->push(function ($message) {
            if(isset($message['MsgType'])){
                $config = config('wechat.mini_program.default');
                switch ($message['MsgType']) {
                    case 'event':
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('大队长客服为您服务'))
                            ->to($message['FromUserName'])->send();
                        break;
                    case 'text':
                        //记录数据库
                        Msgs::create([
                            'openid' => $message['FromUserName'],
                            'content' => $message['Content'],
                            'created_at' => Carbon::now(),
                        ]);
                        //客服消息需要用单独的定时任务做处理，不然会涉及微信重复导致的重复推送
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('您好，大队长客服为您服务。如有需要合作，请留下您的联系方式。'))
                            ->to($message['FromUserName'])->send();
                        break;
                    case 'image':
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('收到图片消息'))
                            ->to($message['FromUserName'])->send();
                        break;
                    case 'voice':
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('收到语音消息'))
                            ->to($message['FromUserName'])->send();
                        break;
                    case 'video':
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('收到视频消息'))
                            ->to($message['FromUserName'])->send();
                        break;
                    case 'location':
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('收到坐标消息'))
                            ->to($message['FromUserName'])->send();
                        break;
                    case 'link':
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('收到链接消息'))
                            ->to($message['FromUserName'])->send();
                        break;
                    case 'file':
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('收到文件消息'))
                            ->to($message['FromUserName'])->send();
                        break;
                    default:
                        $app = Factory::miniProgram($config);
                        $app->customer_service->message(new Text('收到其它消息'))
                            ->to($message['FromUserName'])->send();
                        break;
                }
                echo 'success';
                die();
            }
        });
        $response = $app->server->serve();
        return $response;
    }



}
